package 树;

import binary_tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 层次遍历
 * @author macy
 * @date 2025/11/10 15:20
 */
public class LevelTraverseTreeNode {

    public static void main(String[] args) {

    }


    public void levelTraverse(TreeNode root){
        if(root==null){
            return;
        }

        //创建队列
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            int sz = queue.size();
            // 从左到右遍历每一层的每个节点
            for (int i = 0; i < sz; i++) {
                TreeNode node=queue.poll();
                System.out.println(node.val);
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }
        }
    }

}
